<template>
  <div class="app-container">
<!--    <el-row :gutter="20">-->
      <!--      &lt;!&ndash;部门数据&ndash;&gt;
            <el-col :span="4" :xs="24">
              <div class="head-container">
                <el-input
                  v-model="deptName"
                  placeholder="请输入部门名称"
                  clearable
                  size="small"
                  prefix-icon="el-icon-search"
                  style="margin-bottom: 20px"
                />
              </div>
              <div class="head-container">
                <el-tree
                  :data="deptOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="filterNode"
                  ref="tree"
                  default-expand-all
                  @node-click="handleNodeClick"
                />
              </div>
            </el-col>-->
      <table>
        <tr>
          <td><span>模 板 名 称 :</span></td>
          <td><el-input v-model="form.template_name" placeholder="请输入文档标题" style="width: 240px"></el-input></td>

        </tr>
        <br>
        <tr>
          <td>模板风格:</td>
          <td><el-input v-model="form.template_type" placeholder="请输入模板风格" style="width: 240px"></el-input></td>
        </tr>
        <br>
        <tr>
          <td>上传模板:</td>
          <td><el-upload class="avatar-uploader"
                         :action="''"
                         :auto-upload="false"
                         :show-file-list="false"
                         :on-change="handleAccessoryChangeIcon">
            <span v-if="form.accessory" style="height: 150px;">{{form.accessory}}</span>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload></td>
        </tr>
        <br>
        <tr>
          <td><span>文档所属频道:</span></td>
          <td  style="position: absolute;z-index: 10001;">

              <el-select v-model="form.channel_id" placeholder="请选择">
                <!--<el-option

                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>-->
                <el-option
                  v-for="item in option"
                  :key="item.channel_id"
                  :label="item.channel_name"
                  :value="item.channel_id"
                ></el-option>
              </el-select>
          </td>
        </tr>
        <br>
        <tr>
          <td><span>文档状态:</span></td>
          <td>
            <el-select v-model="form.status" placeholder="文档状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dict_value"
                :label="dict.dict_label"
                :value="dict.dict_value"
              />
            </el-select>
          </td>
        </tr>
        <br>
        <br>
        <tr>
          <td><span>文档内容:</span></td>
          <td>
              <editor style="position: relative;z-index: 0;" ref="editorOne" v-model="form.template_content"  @change="change"></editor>
          </td>

        </tr>
        <br>
        <br>
        <tr>
          <td></td>
          <td>
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button type="info" @click="reset" style="margin-left: 30px;">重置</el-button></td>
        </tr>
      </table>

<!--    </el-row>-->
  </div>
</template>

<script>


  import { listTemplate ,delTemplate ,uploadHtml ,addTemplate , getTemplate ,updateTemplate} from "@/api/monitor/template";
  import { listChannel , getChannel , addChannel , updateChannel ,delChannel,getParentChannel} from "@/api/monitor/channel";
  import { getToken } from "@/utils/auth";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  import Editor from '@/components/wangEditor'

  export default {
    name: "File",
    components: { Treeselect  ,Editor },
    data() {
      return {
        isClear: false,
        detail:"",
        user: {},
        sexs: [],
        option:[],
        id_card_type: [],
        status:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 默认密码
        initPassword: undefined,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 性别状态字典
        Options: [],
        // 岗位选项
        postOptions: [],
        // 角色选项
        roleOptions: [],
        // 表单参数
        form: {
          template_content: null,
        },
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,

        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 1000,
          name: undefined,
          status: undefined
        },
        /*        // 表单校验
                rules: {
                  userName: [
                    { required: true, message: "用户名称不能为空", trigger: "blur" }
                  ],
                  nickName: [
                    { required: true, message: "用户昵称不能为空", trigger: "blur" }
                  ],

                  email: [
                    {
                      type: "email",
                      message: "'请输入正确的邮箱地址",
                      trigger: ["blur", "change"]
                    }
                  ],
                  phonenumber: [
                    {
                      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                      message: "请输入正确的手机号码",
                      trigger: "blur"
                    }
                  ]
                }*/
      };
    },
    created() {
      const templateId = this.$route.params && this.$route.params.templateId;
      this.getTemplate(templateId);
      this.getListChannle();
      this.getDicts("sys_template_status").then(response => {
        this.statusOptions = response.data.items;
      });
    },

    methods: {

      getTemplate(templateId){
        getTemplate(templateId).then(rs =>{
          this.form = rs.data;
        })
      },


      change(val) {
        console.log(val)

      },
      submit () {
        updateTemplate(this.form).then(rs =>{
          this.msgSuccess("修改成功");
        })
      },
      // 表单重置
      reset() {
        this.form = {
          file_title: undefined,
          file_content: undefined,
          template_id: undefined,
          channel_id: undefined,
        };
        this.resetForm("form");
      },
      //获取频道
      getListChannle(){
        getParentChannel(this.addDateRange(this.queryParams, this.dateRange)).then(rs =>{
          this.option = rs.data.items;
        })
      },
      /*文档附件上传*/
      handleAccessoryChangeIcon(file, fileList) {//选中文件触发的change事件
         console.log(file)
/*        const isJPG = file.raw.type === 'image/jpeg'
        const isPNG = file.raw.type === 'image/png'
        // const isLt2M = file.raw.size / 1024 / 1024 < 0.5  //限制上传文件的大小
        if (!isPNG && !isJPG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!')
          return false
        } else {*/
          this.form.accessory = file.name;//赋值图片的url，用于图片回显功能
          this.uploadHtml(file)//调用上传文件api接口
          this.$forceUpdate()

      },
      uploadHtml(file){//文件上传操作
        let formData=new FormData()
        formData.append('file',file.raw)
        uploadHtml(formData).then(res=>{
          console.log(res)
          if (res.code == '20000') {
            // editor.txt.append(res.data.template_content)
            console.log(res.data.template_content)
            // this.form.template_content =
            //   `<pre><code class="Html"><xmp>` + res.data.template_content.replace(/\r\n/g,"</xmp><xmp>").replace(/\n/g,"</xmp><xmp>") + `</xmp></code></pre>`

            this.form.template_content =
              `<pre style="display: block;padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;word-break: break-all;word-wrap: break-word;background-color: #f5f5f5;border: 1px solid #ccc;border-radius: 4px;overflow: auto;"><code class="Html"><xmp>` + res.data.template_content.replace(/>/g,"></xmp><xmp>") + `</xmp></code></pre>`
          }
        })
      },

    }

  };
</script>

<style scoped>
  .app-container table{
    width: 100%;
    max-width: 100%;
  }

  .app-container table>tr{
    width: 100%;
    max-width: 100%;
  }

.app-container table>tr>td:first-of-type{
  width: 120px;
  min-width: 120px;
}

.app-container table>tr>td:last-of-type{
  width: calc(100% - 500px);
  max-width: calc(100% - 500px);
}
</style>
